package com.hzhq.contract.controller;

import com.hzhq.contract.bean.entity.Admin;
import com.hzhq.contract.bean.vo.AdminVO;
import com.hzhq.contract.convert.AdminConvertBasic;
import com.hzhq.contract.result.Result;
import com.hzhq.contract.service.AdminService;
import com.hzhq.contract.util.Page2ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author: hzhq1255
 * @mail: hzhq1255@163.com
 * @date: 2021/4/14 22:25
 * @desc:
 */
@Slf4j
@Api(tags = "系统管理员")
@CrossOrigin
@RestController
@ResponseBody
@RequestMapping("/api/v1/admins")
public class AdminController {



    @Autowired
    AdminService adminService;


    @ApiOperation(value = "通过id获取管理员")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "adminId",value = "adminId", paramType = "path", dataTypeClass = String.class, dataType = "String")
    )
    @RequestMapping(value = "/adminId/{adminId}",method = RequestMethod.GET)
    private Result<Object> getOneByAdminId(@PathVariable("adminId") String adminId){
        AdminVO adminVO = AdminConvertBasic.INSTANCE.dtoToVo(adminService.getOneByAdminId(new ObjectId(adminId)));
        return Result.success(adminVO);
    }


    @ApiOperation(value = "通过name获取管理员")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "adminName",value = "adminName", paramType = "path", dataTypeClass = String.class, dataType = "String")
    )
    @RequestMapping(value = "/adminName/{adminName}",method = RequestMethod.GET)
    private Result<Object> getOneByAdminName(@PathVariable("adminName") String adminName){
        AdminVO adminVO = AdminConvertBasic.INSTANCE.dtoToVo(adminService.getOneByAdminName(adminName));
        return Result.success(adminVO);
    }


    @ApiOperation(value = "获取所有管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "path", dataTypeClass = Integer.class, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页个数", paramType = "path", dataTypeClass = Integer.class, dataType = "Integer")
    })
    @RequestMapping(value = "/currentPage/{currentPage}/pageSize/{pageSize}",method = RequestMethod.GET)
    private Result<Object> getOneByAdminName(@PathVariable("currentPage") Integer currentPage,
                                             @PathVariable("pageSize") Integer pageSize){
        Page<AdminVO> adminVOPage = adminService.getAllPage(currentPage, pageSize).map(AdminConvertBasic.INSTANCE::dtoToVo);
        return Result.success(Page2ResultUtil.getPageData(adminVOPage));
    }






}
